using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using AutoUpdateClient.Sstc.UpdateService;

namespace AutoUpdateClient
{
    /// <summary>
    /// send some messages back to GUI
    /// </summary>
    /// <param name="msg"></param>
    public delegate void  ReportProgress(string msg);

    public class Client
    {

        public Client()
        {
            service = new UpdateService();
        }

        /// <summary>
        /// a delegate used to send msg back to GUI
        /// </summary>
        public ReportProgress Report
        {
            get
            {
                return report;
            }
            set
            {
                report = value;
            }
        }

        /// <summary>
        /// download update engine if it has a new version published 
        /// </summary>    
        public void UpdateEngine(string enginePath)
        {
            try
            {
                string localEngineVersion = GetLocalEngineVersion(enginePath);
                if (service.NeedToUpdateEngine(localEngineVersion))
                {
                    if (report != null)
                        report("download update engine...");

                    byte[] engineData = service.GetNewEngine();
                    //delete the old engine if it exists
                    if (File.Exists(enginePath))
                        File.Delete(enginePath);

                    File.WriteAllBytes(enginePath, engineData);

                    if (report != null)
                        report("download update engine finished!");
                }
                else
                {
                    report("engine is new ");
                }
            }
            catch (Exception ex)
            {
                report(ex.Message);
            }
        }
       /// <summary>
       /// call auto-update engine to execute
       /// </summary>
        /// <param name="enginePath"></param>
        /// <param name="defaultWorkingDirectory"></param>
        public void StartEngine(string enginePath,string defaultWorkingDirectory)
        {
            try
            {
                ProcessStartInfo pinfo = new ProcessStartInfo(enginePath, defaultWorkingDirectory);
                Process.Start(pinfo);
            }
            catch (Exception ex)
            {
                report(ex.Message);
            }
        }
        /// <summary>
        /// get the version of the update engine assembly installed on user's local disks
        /// </summary>
        /// <param name="enginePath"></param>
        /// <returns></returns>
        private static string GetLocalEngineVersion(string enginePath)
        {
            if (!File.Exists(enginePath))
                return "0.0.0.0";

            return System.Reflection.Assembly.LoadFile(enginePath).GetName().Version.ToString();
        }

        UpdateService service;
        ReportProgress report;
    }
}
